'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class IcqContact
    Inherits BaseStorageItem

    Implements Interfaces.IContact
    Implements IComparable, IComparable(Of IcqContact)

    Private _Group As Interfaces.IGroup

    Public Sub New()

    End Sub

    Public Sub New(ByVal id As String, ByVal name As String)
        MyBase.New(id, name)
        Attributes("Status") = IcqStatusCodes.Offline
    End Sub

    Friend Sub SetGroup(ByVal group As Interfaces.IGroup)
        _Group = group
    End Sub

    Public Property ItemId() As Integer
        Get
            Return CInt(Attributes("ItemId"))
        End Get
        Set(ByVal value As Integer)
            Attributes("ItemId") = value
            OnPropertyChanged("ItemId")
        End Set
    End Property

    Public Property DenyRecordItemId() As Integer
        Get
            Return CInt(Attributes("DenyRecordItemId"))
        End Get
        Set(ByVal value As Integer)
            Attributes("DenyRecordItemId") = value
            OnPropertyChanged("DenyRecordItemId")
        End Set
    End Property

    Public Property PermitRecordItemId() As Integer
        Get
            Return CInt(Attributes("PermitRecordItemId"))
        End Get
        Set(ByVal value As Integer)
            Attributes("PermitRecordItemId") = value
            OnPropertyChanged("PermitRecordItemId")
        End Set
    End Property

    Public Property IgnoreRecordItemId() As Integer
        Get
            Return CInt(Attributes("IgnoreRecordItemId"))
        End Get
        Set(ByVal value As Integer)
            Attributes("IgnoreRecordItemId") = value
            OnPropertyChanged("IgnoreRecordItemId")
        End Set
    End Property

    Public Property MemberSince() As DateTime Implements Interfaces.IContact.MemberSince
        Get
            Return CDate(Attributes("MemberSince"))
        End Get
        Set(ByVal value As DateTime)
            Attributes("MemberSince") = value
            OnPropertyChanged("MemberSince")
        End Set
    End Property

    Public Property SignOnTime() As DateTime Implements Interfaces.IContact.SignOnTime
        Get
            Return CDate(Attributes("SignOnTime"))
        End Get
        Set(ByVal value As DateTime)
            Attributes("SignOnTime") = value
            OnPropertyChanged("SignOnTime")
        End Set
    End Property

    Public Property FirstName() As String Implements Interfaces.IContact.FirstName
        Get
            Return CStr(Attributes("FirstName"))
        End Get
        Set(ByVal value As String)
            Attributes("FirstName") = value
            OnPropertyChanged("FirstName")
        End Set
    End Property

    Public Property LastName() As String Implements Interfaces.IContact.LastName
        Get
            Return CStr(Attributes("LastName"))
        End Get
        Set(ByVal value As String)
            Attributes("LastName") = value
            OnPropertyChanged("LastName")
        End Set
    End Property

    Public Property EmailAddress() As String Implements Interfaces.IContact.EmailAddress
        Get
            Return CStr(Attributes("EmailAddress"))
        End Get
        Set(ByVal value As String)
            Attributes("EmailAddress") = value
            OnPropertyChanged("EmailAddress")
        End Set
    End Property

    Public Property Gender() As Interfaces.ContactGender Implements Interfaces.IContact.Gender
        Get
            Dim value As Byte = CByte(Attributes("Gender"))

            Select Case value
                Case 1
                    Return Interfaces.ContactGender.Male
                Case 2
                    Return Interfaces.ContactGender.Female
                Case Else
                    Return Interfaces.ContactGender.Unknown
            End Select
        End Get
        Set(ByVal value As Interfaces.ContactGender)
            Attributes("Gender") = value
            OnPropertyChanged("Gender")
        End Set
    End Property

    Public Property AuthorizationRequired() As Boolean Implements Interfaces.IContact.AuthorizationRequired
        Get
            Return CBool(Attributes("AuthorizationRequired"))
        End Get
        Set(ByVal value As Boolean)
            Attributes("AuthorizationRequired") = value
            OnPropertyChanged("AuthorizationRequired")
        End Set
    End Property

    Public ReadOnly Property IconHash() As List(Of Byte) Implements Interfaces.IContact.IconHash
        Get
            If Attributes("IconHash") Is Nothing Then Return Nothing

            Return DirectCast(Attributes("IconHash"), List(Of Byte))
        End Get
    End Property

    Public ReadOnly Property IconData() As List(Of Byte) Implements Interfaces.IContact.IconData
        Get
            If Attributes("IconData") Is Nothing Then Return Nothing

            Return DirectCast(Attributes("IconData"), List(Of Byte))
        End Get
    End Property

    Friend Sub SetIconHash(ByVal value As List(Of Byte)) Implements Interfaces.IContact.SetIconHash
        Dim oldValue As List(Of Byte)

        oldValue = IconHash

        If IconData IsNot Nothing AndAlso CompareLists(oldValue, value) Then Return

        Attributes("IconHash") = value

        Core.Kernel.Logger.Log("IcqContact", TraceEventType.Information, "Received new icon hash for {0}", Identifier)

        RaiseEvent IconHashReceived(Me, EventArgs.Empty)
    End Sub

    Private Function CompareLists(ByVal left As List(Of Byte), ByVal right As List(Of Byte)) As Boolean
        Dim ln As Boolean = left Is Nothing
        Dim rn As Boolean = right Is Nothing
        Dim lcount As Integer
        Dim rcount As Integer

        If ln And rn Then Return True
        If ln <> rn Then Return False

        lcount = left.Count
        rcount = right.Count

        If lcount = 0 And rcount = 0 Then Return True
        If lcount <> rcount Then Return False

        For i = 0 To lcount - 1
            If left(i) <> right(i) Then Return False
        Next

        Return True
    End Function

    Friend Sub SetIconData(ByVal value As List(Of Byte)) Implements Interfaces.IContact.SetIconData
        Dim oldValue As List(Of Byte)

        oldValue = IconData

        If CompareLists(oldValue, value) Then Return

        Attributes("IconData") = value

        Core.Kernel.Logger.Log("IcqContact", TraceEventType.Information, "Received new icon data for {0}", Identifier)

        RaiseEvent IconDataReceived(Me, EventArgs.Empty)
    End Sub

    Public Property LastShortUserInfoRequest() As Date
        Get
            Return CDate(Attributes("LastShortUserInfoRequest"))
        End Get
        Set(ByVal value As Date)
            Attributes("LastShortUserInfoRequest") = value
        End Set
    End Property

    Public ReadOnly Property Group() As Interfaces.IGroup Implements Interfaces.IContact.Group
        Get
            Return _Group
        End Get
    End Property

    Public Property Status() As Interfaces.IStatusCode Implements Interfaces.IContact.Status
        Get
            Return DirectCast(Attributes("Status"), Interfaces.IStatusCode)
        End Get
        Set(ByVal value As Interfaces.IStatusCode)
            If Status IsNot value Then
                Attributes("Status") = value
                OnPropertyChanged("Status")
            End If
        End Set
    End Property

    Public Function CompareTo(ByVal other As IcqContact) As Integer Implements System.IComparable(Of IcqContact).CompareTo
        Dim x As Integer

        x = Comparer.Default.Compare(Identifier, other.Identifier)

        If x = 0 Then
            x = Status.CompareTo(other.Status)
        End If

        Return x
    End Function

    Public Function CompareTo1(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
        Dim x As IcqContact = TryCast(obj, IcqContact)

        If x IsNot Nothing Then Return CompareTo(x)
    End Function

    Public Event IconDataReceived(ByVal sender As Object, ByVal e As System.EventArgs) Implements Interfaces.IContact.IconDataReceived

    Public Event IconHashReceived(ByVal sender As Object, ByVal e As System.EventArgs) Implements Interfaces.IContact.IconHashReceived

End Class